package com.lzy.cloud.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.cloud.base.dto.BaseCouponDto;
import com.lzy.cloud.base.entity.BaseCoupon;
import com.lzy.cloud.base.mapper.BaseCouponMapperExt;
import com.lzy.cloud.base.service.CouponService;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private BaseCouponMapperExt baseCouponMapperExt;

    @Override
    public Long insert(BaseCouponDto baseCouponDto) {
        BaseCoupon baseCoupon = DozerUtils.map(baseCouponDto, BaseCoupon.class);
        return baseCouponMapperExt.insertSelective(baseCoupon)>0?baseCoupon.getId():0;
    }

    @Override
    public boolean update(BaseCouponDto baseCouponDto) {
        return baseCouponMapperExt.updateByPrimaryKeySelective(DozerUtils.map(baseCouponDto, BaseCoupon.class)) > 0;
    }

    @Override
    public BaseCouponDto find(Long id) {
        return DozerUtils.map(baseCouponMapperExt.selectByPrimaryKey(id), BaseCouponDto.class);
    }

    @Override
    public PageInfo<BaseCouponDto> queryList(Pager pager, BaseCouponDto baseCouponDto) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        return new PageInfo<>(baseCouponMapperExt.queryList(baseCouponDto));
    }

    @Override
    public boolean deleteById(Long id) {
        BaseCouponDto baseCouponDto = new BaseCouponDto();
        baseCouponDto.setId(id);
        baseCouponDto.setDeleted(true);
        return baseCouponMapperExt.updateByPrimaryKeySelective(DozerUtils.map(baseCouponDto, BaseCoupon.class)) > 0;
    }

    @Override
    public List<BaseCouponDto> queryList(BaseCouponDto baseCouponDto) {
        return baseCouponMapperExt.queryList(baseCouponDto);
    }

    @Override
    public List<BaseCouponDto> findCarRentByIds(List<Long> ids) {
        String idsStr = "";
        for (int i = 0; i < ids.size(); i++) {
            if (i == ids.size() - 1) {
                idsStr += ids.get(i);
            } else {
                idsStr += ids.get(i) + ",";
            }
        }
        return baseCouponMapperExt.findCarRentByIds(idsStr);
    }

    @Override
    public List<BaseCouponDto> selectNotExpiredCoupon(String categorys) {
        return baseCouponMapperExt.selectNotExpiredCoupon(categorys);
    }

    @Override
    public List<BaseCouponDto> selectBindCoupon(String categorys) {
        return baseCouponMapperExt.selectBindCoupon(categorys);
    }

}
